package sequence

import (
	"github.com/zeromicro/go-zero/core/logx"
	"github.com/zeromicro/go-zero/core/stores/cache"
	"github.com/zeromicro/go-zero/core/stores/redis"
)

const SequenceKey = "seq:id"

type Redis struct {
	Client *redis.Redis
}

func NewRedis(CacheRedis cache.CacheConf) Sequence {
	return Redis{
		Client: redis.MustNewRedis(CacheRedis[0].RedisConf),
	}
}

func (r Redis) Next() (seq uint64, err error) {
	res, err := r.Client.Incr(SequenceKey)
	if err != nil {
		logx.Errorw("redis incr err", logx.Field("err", err))
		return 0, err
	}
	return uint64(res), nil
}
